﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using Adp.Common;
using Adp;
using Yqd.Controllers;
using Yqd.Models;

namespace Bmzx.Controllers
{
    public class CommunityController : BaseController
    {
        YqdContext ctx;

        public CommunityController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        public IActionResult GetList()
        {
            var start = Form.GetInt("start");
            var limit = Form.GetInt("limit");
            var StreetID = Form.GetInt("StreetID");
            var key = Form.GetString("searchkey");
            var where = "1=1";
            if (StreetID > 0)
            {
                where += " and StreetID= {0}";
            }
            if (key.Trim() != "")
            {
                where += " and lower(ltrim(rtrim([Name]))) like {1}";
            }
            var (totalcount, data) = ctx.Database.SqlPager<Yqd_Community>(start, limit, "*", where, "[Tactic] desc,[CommunityID]", StreetID, "%" + key.ToLower().Trim() + "%");
            var zjlist = data.Select(m => m.StreetID);
            var ZJ = ctx.Yqd_Street.Where(it => zjlist.Contains(it.StreetID)).ToList();
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = data.Select(m => new
                {
                    m.CommunityID,
                    m.Name,
                    Street = ZJ.FirstOrDefault(it => it.StreetID == m.StreetID)?.Name,
                    m.Tactic,
                    Instime = m.Instime.ToString("yyyy-MM-dd")
                })
            });
        }

        public IActionResult Add(Yqd_Community model)
        {
            if (!ModelState.IsValid)
            {
                return this.ToJson(new
                {
                    success = false,
                    stateMsg = from sk in ModelState.AsEnumerable()
                               where sk.Value.Errors.Count() > 0
                               select new { id = sk.Key, msg = sk.Value.Errors.FirstOrDefault().ErrorMessage }
                });
            }
            model.Instime = DateTime.Now;
            model.Creator = DAL.GetLogonInfo().ManagerID;
            ctx.Yqd_Community.Add(model);
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true,
                ret
            });
        }

        public IActionResult Edit(Yqd_Community model)
        {
            if (!ModelState.IsValid)
            {
                return this.ToJson(new
                {
                    success = false,
                    stateMsg = from sk in ModelState.AsEnumerable()
                               where sk.Value.Errors.Count() > 0
                               select new { id = sk.Key, msg = sk.Value.Errors.FirstOrDefault().ErrorMessage }
                });
            }
            try
            {
                var m = ctx.Yqd_Community.FirstOrDefault(it => it.CommunityID == model.CommunityID);
                if (m != null)
                {
                    m.Name = model.Name;
                    m.StreetID = model.StreetID;
                    m.Remark = model.Remark;
                }
                var ret = ctx.SaveChanges();
                return this.ToJson(new
                {
                    success = true,
                    ret
                });
            }
            catch
            {
                return this.ToJson(new
                {
                    success = false,
                    msg = "社区不存在",
                });
            }
        }

        public ActionResult Get()
        {
            var CommunityID = Form.GetInt("CommunityID");
            var m = ctx.Yqd_Community.FirstOrDefault(it => it.CommunityID == CommunityID);
            if (m != null)
            {
                return this.ToJson(new
                {
                    success = true,
                    data = new
                    {
                        m.CommunityID,
                        m.Name,
                        m.StreetID,
                        m.Remark
                    }
                });
            }
            return this.ToJson(new
            {
                success = false,
                msg = "社区不存在"
            });
        }

        public IActionResult NameExists()
        {
            var StreetID = Form.GetInt("StreetID");
            var CommunityID = Form.GetInt("CommunityID");
            var name = Form.GetString("name");
            return this.ToJson(new
            {
                success = ctx.Yqd_Community.Any(it =>it.StreetID == StreetID && it.Name.ToLower().Trim() == name.ToLower().Trim() && it.CommunityID != CommunityID)
            });
        }

        public ActionResult UpdateTop()
        {
            var CommunityID = Form.GetInt("CommunityID");
            var tops = Form.GetInt("tops");
            if (CommunityID > 0)
            {
                var ret = ctx.Database.ExecuteSql("update Yqd_Community set Tactic={1} where CommunityID ={0}", CommunityID, tops);
                return this.ToJson(new
                {
                    success = true,
                    ret
                });
            }
            return this.ToJson(new
            {
                success = false,
                msg = "社区不存在"
            });
        }

        public ActionResult Delete()
        {
            var CommunityIDs = Form.GetIntArray("CommunityID[]");
            if (CommunityIDs.Length > 0)
            {
                var sql = string.Format("delete from Yqd_Community where CommunityID in({0});", string.Join(',', CommunityIDs));
                var ret = ctx.Database.ExecuteSql(sql);
                return this.ToJson(new
                {
                    success = true,
                    ret
                });
            }
            return this.ToJson(new
            {
                success = false,
                msg = "社区不存在"
            });
        }
    }
}